package edu.sysu.cshare.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import edu.sysu.cshare.control.MessageManager;
import edu.sysu.cshare.model.Message;
import edu.sysu.cshare.model.Message.Category;

import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.Insets;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.JComboBox;

public class Dlg_Msg extends JDialog {
	private Message message;

	public Message getMessage() {
		return message;
	}

	private boolean createNew = false;
	private MessageManager messageManager;
	private final JPanel contentPanel = new JPanel();
	private String attachLocalPath;
	private JTextField txtTitle;
	private JComboBox cmboxCategory;
	private JEditorPane txtContent;

	/**
	 * Create the dialog.
	 */
	public Dlg_Msg(MessageManager messageManager, Message message) {
		this.messageManager = messageManager;
		this.message = message;
		if (message == null)
			createNew = true;
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());

		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 0, 0, 0, 0 };
		gbl_panel.rowHeights = new int[] { 0, 0, 0, 0 };
		gbl_panel.columnWeights = new double[] { 0.0, 1.0, 1.0,
				Double.MIN_VALUE };
		gbl_panel.rowWeights = new double[] { 0.0, 0.0, 1.0, Double.MIN_VALUE };
		panel.setLayout(gbl_panel);

		JLabel lblTitle = new JLabel("标题");
		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.insets = new Insets(0, 0, 5, 5);
		gbc_lblTitle.anchor = GridBagConstraints.EAST;
		gbc_lblTitle.gridx = 0;
		gbc_lblTitle.gridy = 0;
		panel.add(lblTitle, gbc_lblTitle);

		txtTitle = new JTextField();
		GridBagConstraints gbc_txtTitle = new GridBagConstraints();
		gbc_txtTitle.insets = new Insets(0, 0, 5, 5);
		gbc_txtTitle.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTitle.gridx = 1;
		gbc_txtTitle.gridy = 0;
		panel.add(txtTitle, gbc_txtTitle);
		txtTitle.setColumns(10);

		cmboxCategory = new JComboBox();
		for (Message.Category c : Message.Category.values()) {
			if (c != Category.All) {
				cmboxCategory.addItem(c);
			}
		}
		GridBagConstraints gbc_cmboxCategory = new GridBagConstraints();
		gbc_cmboxCategory.insets = new Insets(0, 0, 5, 0);
		gbc_cmboxCategory.fill = GridBagConstraints.HORIZONTAL;
		gbc_cmboxCategory.gridx = 2;
		gbc_cmboxCategory.gridy = 0;
		panel.add(cmboxCategory, gbc_cmboxCategory);

		JLabel lblNewLabel_1 = new JLabel("内容");
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_1.gridx = 0;
		gbc_lblNewLabel_1.gridy = 1;
		panel.add(lblNewLabel_1, gbc_lblNewLabel_1);

		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridwidth = 3;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 2;
		panel.add(scrollPane, gbc_scrollPane);

		txtContent = new JEditorPane();
		scrollPane.setViewportView(txtContent);

		JPanel panel_1 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		getContentPane().add(panel_1, BorderLayout.SOUTH);

		JButton btnNewButton = new JButton("附件");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (createNew) {
					// 上传
				} else {
					// 下载
				}
			}
		});
		panel_1.add(btnNewButton);

		JButton btnSave = new JButton("确定");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Message msg = new Message(txtTitle.getText(),
						Dlg_Msg.this.messageManager.getActor(), (Category)cmboxCategory.getSelectedItem(),
						txtContent.getText());
				if (attachLocalPath != null) {
					try {
						Dlg_Msg.this.messageManager.announce(msg, attachLocalPath);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				} else {
					Dlg_Msg.this.messageManager.announce(msg);
				}
				JOptionPane.showMessageDialog(null, "发布成功");
				Dlg_Msg.this.dispose();
			}
		});
		panel_1.add(btnSave);

	}

}
